const Brand = require("../models/brand");
const { returnFn } = require("../config/returnFn");

const checkRootLogin = (req, res, next) => {
    if (!req.session.employee) {
        return returnFn(res, 400, "未登录");
    }
    next();
}

const addSecondCategory = (req, res) => {
    let brand = new Brand({
        brandName: req.body.brandName ? req.body.brandName : "",
        categoryId: req.body.categoryId ? req.body.categoryId : "",
        brandLogo: req.body.brandLogo ? req.body.brandLogo : "",
        isDelete: req.body.isDelete ? parseInt(req.body.isDelete) : "",
        hot: req.body.hot ? parseInt(req.body.hot) : "",
    });
    Brand.addSecondCategory(brand, (err, result) => {
        if (err) return returnFn(res, 503, "数据库异常", err);
        returnFn(res, 200, "success");
    })
}
const updateSecondCategory = (req, res) => {
    let brand = new Brand({
        id: req.body.id ? req.body.id : "",
        brandName: req.body.brandName ? req.body.brandName : "",
        categoryId: req.body.categoryId ? req.body.categoryId : "",
        brandLogo: req.body.brandLogo ? req.body.brandLogo : "",
        isDelete: req.body.isDelete ? parseInt(req.body.isDelete) : "",
        hot: req.body.hot ? parseInt(req.body.hot) : "",
    });
}

module.exports = {
    checkRootLogin,
    addSecondCategory,
    updateSecondCategory
}